package com.cats.template.sys.pojo.model;

import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.cats.template.sys.pojo.dto.BaseDto;
import lombok.Getter;
import org.springframework.beans.BeanUtils;

import javax.persistence.Column;
import java.io.Serializable;
import java.time.LocalDateTime;
import java.util.Objects;

/**
 * @author duxiaobo
 * @date 2021/8/94:22 下午
 */
@Getter
public class BaseModel<T> implements Serializable {
    @TableId(value = "id", type = IdType.ID_WORKER)
    private Long id;
    @Column
    private Integer orderNumber;
    @TableField(value = "created_by", fill = FieldFill.INSERT)
    private Long createdBy;
    @TableField(value = "updated_by", fill = FieldFill.INSERT_UPDATE)
    private Long updatedBy;
    @TableField(value = "created_at", fill = FieldFill.INSERT)
    private LocalDateTime createdAt;
    @TableField(value = "updated_at", fill = FieldFill.INSERT_UPDATE)
    private LocalDateTime updatedAt;
    @Column
    private Boolean deleteFlg;

    public <Model, Dto extends BaseDto> Dto parseDto(Class<Dto> clazz) {
        if (Objects.isNull((T) this)) {
            return null;
        } else {
            Dto dto = BeanUtils.instantiateClass(clazz);
            BeanUtils.copyProperties((T) this, dto);
            return dto;
        }
    }

    public T setId(Long id) {
        this.id = id;
        return (T) this;
    }

    public T setCreatedBy(Long createdBy) {
        this.createdBy = createdBy;
        return (T) this;
    }

    public T setUpdatedBy(Long updatedBy) {
        this.updatedBy = updatedBy;
        return (T) this;
    }

    public T setCreatedAt(LocalDateTime createdAt) {
        this.createdAt = createdAt;
        return (T) this;
    }

    public T setUpdatedAt(LocalDateTime updatedAt) {
        this.updatedAt = updatedAt;
        return (T) this;
    }

    public T setDeleteFlg(Boolean deleteFlg) {
        this.deleteFlg = deleteFlg;
        return (T) this;
    }

    public T setOrderNumber(Integer orderNumber) {
        this.orderNumber = orderNumber;
        return (T) this;
    }
}
